package longrise.phone.com.bjjt_jyb.compensate;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerCar;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;

import java.util.Timer;
import java.util.TimerTask;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.Constant.URLConstant;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.ApiImpI;
import longrise.phone.com.bjjt_jyb.Utils.CommontUtils;
import longrise.phone.com.bjjt_jyb.Utils.ICometManager;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;
import longrise.phone.com.bjjt_jyb.Utils.UiUtils;
import longrise.phone.com.bjjt_jyb.Utils.icomet.Channel;
import longrise.phone.com.bjjt_jyb.Utils.icomet.ChannelAllocator;
import longrise.phone.com.bjjt_jyb.Utils.icomet.ICometCallback;
import longrise.phone.com.bjjt_jyb.Utils.icomet.IConnCallback;
import longrise.phone.com.bjjt_jyb.Utils.icomet.message.Message;


/**
 * 事故审核界面，Created by Administrator on 2016/5/12.
 */

public class AccidentCheckView extends LFView implements View.OnClickListener, ILFMsgListener {
    private Context context = null;
    private View view = null;
    private TextView titleTV;
    private ImageView infoIV;//说明图片
    private TextView infoTV;//说明文字
    private LinearLayout waitLayout;//倒计时视图
    private TextView timeTV;
    private LinearLayout dealLayout;//审核后续处理视图
    private Button dealOBtn;
    private Button dealTBtn;
    private int time = 180;//时间数值
    private ICometManager iCometManager;
    private Timer timer;
    private TimerTask task;
    private static final int TIME = 0;//倒计时标识
    private static final int REC_SUCCESS = 1;//审核成功标识
    private static final int REC_FAIL = 2;//审核失败标识
    private static final int SEARCH_MSG = 3;//查询消息标识
    private static final int REC_MSG = 4;//接受消息标识
    private static final int CANCEL_MSG = 5;//撤销案件标识

    private String appCaseno;//案件编号
    private int type;//审核类型（0表示事故审核；1表示非机动车事故审核；2表示等待定责；3非机动车等待定责）
    private BaseApplication application;

    private boolean isFinish = false;//本次案件是否结束处理
    private Dialog processDialog;

    public AccidentCheckView(Context c, String appCaseno, int type) {
        super(c);
        this.context = c;
        this.appCaseno = appCaseno;
        this.type = type;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        setData();
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_accidentcheckview, null);
        if (null == view) {
            return;
        }
        //标题栏
        titleTV = (TextView) view.findViewById(R.id.tv_title);
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFinish) {
                    backHomeView();
                } else {
                    showCancelDialog();
                }
            }
        });
        infoIV = (ImageView) view.findViewById(R.id.check_iv_info);
        infoTV = (TextView) view.findViewById(R.id.check_tv_info);
        waitLayout = (LinearLayout) view.findViewById(R.id.check_layout_wait);
        timeTV = (TextView) view.findViewById(R.id.check_tv_time);
        dealLayout = (LinearLayout) view.findViewById(R.id.check_layout_deal);
        dealOBtn = (Button) view.findViewById(R.id.check_btn_deal1);
        dealTBtn = (Button) view.findViewById(R.id.check_btn_deal2);

        application = (BaseApplication) context.getApplicationContext();
    }

    private void regEvent(boolean b) {
        if (null != dealOBtn) {
            dealOBtn.setOnClickListener(b ? this : null);
        }
        if (null != dealTBtn) {
            dealTBtn.setOnClickListener(b ? this : null);
        }
    }

    //根据类型显示初始界面的内容
    private void setData() {
        addILFMsgListener(this);
        if (type == 0) {//机动车事故审核
            titleTV.setText(R.string.accident_check);
            infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.tra_review));
            infoTV.setText(R.string.accident_check_info);
        } else if (type == 1) {//非机动车事故审核
            titleTV.setText(R.string.accident_check);
            infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.tra_review));
            infoTV.setText(R.string.accident_check_info);
        } else if (type == 2) {//机动车等待定责
            titleTV.setText(R.string.response_wait);
            infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.remain_check));
            infoTV.setText(R.string.response_wait_info);
        } else if (type == 3) {//非机动车等待定责
            titleTV.setText(R.string.response_wait);
            infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.remain_check));
            infoTV.setText(R.string.response_wait_info);
        }
        if (type == 0 || type == 1) {//倒计时界面
            timeTV.setText(String.valueOf(time));
        } else {//处理界面
            waitLayout.setVisibility(View.GONE);
            dealLayout.setVisibility(View.VISIBLE);
            dealOBtn.setVisibility(View.GONE);
            if (type == 2||type == 3) {
                dealTBtn.setText("不等待，直接报警！");
            }
        }
        startListening();
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            if (isFinish) {
                backHomeView();
            } else {
                showCancelDialog();
            }
        }
        return false;
    }

    /**
     * 案件未处理结束则弹出提示框
     */
    private void showCancelDialog() {
        UiUtils.showDialog(context, R.mipmap.warn, null, "确定结束本次快处服务吗？", "取消", "确定",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        backHomeView();
                    }
                });
    }

    /**
     * 案件处理结束，回到主界面
     */
    private void backHomeView() {
        FrameworkManager.getInstance().destroyAllForm();
        HomePageView homepageview = new HomePageView(context);
        FrameworkManager.getInstance().showNewForm(context, homepageview);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.check_btn_deal1:
                Object obj = v.getTag();
                if (obj instanceof EntityBean) {
                    EntityBean bean = (EntityBean) obj;
                    EntityBean[] unBeans = bean.getBean("unqualifylist").getBeans("key");
                    EntityBean[] isBeans = bean.getBean("isqualifylist").getBeans("key");
                    String isSingle = bean.getString("issingle");//区分单双车
                    if (type == 0) {//定责图片重新拍照
                        if (TextUtils.equals("0", isSingle)) {//单车
                            AccidentLoadingPictureView sView = new AccidentLoadingPictureView(context, appCaseno, 1,0);
                            sView.setUnqualifylist(unBeans);
                            sView.setIsqualifylist(isBeans);
                            showForm(sView);
                        } else {//双车
                            AccidentLoadingPictureView picView = new AccidentLoadingPictureView(context, appCaseno, 1,1);
                            picView.setUnqualifylist(unBeans);
                            picView.setIsqualifylist(isBeans);
                            showForm(picView);
                        }
                    } else if(type==1){
                        if (TextUtils.equals("11", isSingle)) {//非机动车与非机动车
                            AccidentLoadingPictureView sView = new AccidentLoadingPictureView(context, appCaseno, 1,3);
                            sView.setUnqualifylist(unBeans);
                            sView.setIsqualifylist(isBeans);
                            showForm(sView);
                        } else {//机动车与非机动车
                            AccidentLoadingPictureView picView = new AccidentLoadingPictureView(context, appCaseno, 1,2);
                            picView.setUnqualifylist(unBeans);
                            picView.setIsqualifylist(isBeans);
                            showForm(picView);
                        }

                    }else {//定损图片重新拍照
                       /* if (TextUtils.equals("0", isSingle)) {//单车
                            SingleCarLossView lossView = new SingleCarLossView(context, appCaseno, 1);
                            lossView.setUnqualifylist(unBeans);
                            lossView.setIsqualifylist(isBeans);
                            showForm(lossView);
                        } else {//双车
                            LoadingCarLossView sView = new LoadingCarLossView(context, appCaseno, 1);
                            sView.setUnqualifylist(unBeans);
                            sView.setIsqualifylist(isBeans);
                            showForm(sView);
                        }*/
                    }
                    OnDestroy();
                    closeForm();
                } else {//继续等待
                    waitLayout.setVisibility(View.VISIBLE);
                    dealLayout.setVisibility(View.GONE);
                    startTimer();
                }
                break;
            case R.id.check_btn_deal2:
                //直接报警
                    /*Intent intent = new Intent(Intent.ACTION_DIAL, Uri
                            .parse("tel:122"));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(intent);*/
                    callPolice(appCaseno);

                    /*if(2==type){
                        callPolice(appCaseno);
                    }else {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri
                                .parse("tel:122"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        getContext().startActivity(intent);
                    }
                }*/

                break;
        }
    }
    /**
     * 报警
     *
     *
     */
    private void callPolice(String appCaseno) {
        processDialog = UiUtil.showProcessDialog(context, "加载数据中...");
        EntityBean bean = new EntityBean();
        // 经纬度
        bean.set("appcaseno", appCaseno);
        bean.set("userflag", application.getKckpName());
        bean.set("token", application.getToken());
        LoadDataManagerCar.getInstance(context).callService(null,
                ((BaseApplication) context.getApplicationContext()).getServerUrl(), URLConstant.JAPPALARMDIRECTLY,
                bean, new LoadDataManagerFather.OnRequestCompleteListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        if (null != processDialog) {
                            processDialog.cancel();
                        }
                        if (null != result) {
                            String restate = ((EntityBean) result).getString("restate");
                            String redes = ((EntityBean) result).getString("redes");
                            if ("1".equals(restate)) {
                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri
                                        .parse("tel:110"));
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                getContext().startActivity(intent);
                                closeAllForm();
                                OnDestroy();
                                HomePageView homepageview = new HomePageView(context);
                                FrameworkManager.getInstance().showNewForm(context, homepageview);
                            } else {
                                Toast.makeText(context, redes, Toast.LENGTH_SHORT).show();
                            }
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (null != processDialog) {
                            processDialog.cancel();
                        }
                        Toast.makeText(context, "数据加载失败，请检查网络是否连接", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        if (null != processDialog) {
                            processDialog.cancel();
                        }
                    }
                });
    }

    //实例化即时通讯的接收
    private void startListening() {
        if (null == iCometManager) {
            iCometManager = new ICometManager(connCallback, cometCallback, channel);
            iCometManager.startIMConnect();
        }
        startTimer();
    }

    //结束监听
    private void stopListening() {
        stopTimer();
        if (null != iCometManager) {
            synchronized (iCometManager) {
                iCometManager.stopIMConnect();
            }
        }
        iCometManager = null;
    }

    /**
     * 连接的回调接口
     */
    private IConnCallback connCallback = new IConnCallback() {
        @Override
        public void onSuccess() {
            Log.i("connect", "onSuccess---");
            // 连接成功后循环获取数据
            if (null != iCometManager) {
                iCometManager.comet();
            }
            if (null != handler) {
                handler.sendEmptyMessage(SEARCH_MSG);
            }
        }

        @Override
        public void onStop() {
        }

        @Override
        public void onReconnectSuccess(int arg0) {
            Log.i("connect", "onReconnectSuccess---");
            if (null != iCometManager) {
                iCometManager.comet();
            }
            if (null != handler) {
                handler.sendEmptyMessage(SEARCH_MSG);
            }
        }

        @Override
        public boolean onReconnect(int arg0) {
            return false;
        }

        @Override
        public void onFail(String arg0) {
            Log.i("connect", "onFail---" + arg0);
//            Looper.prepare();
//            Toast.makeText(context, "服务连接异常，请检查网络!", Toast.LENGTH_SHORT).show();
//            Looper.loop();
        }

        @Override
        public void onDisconnect() {
        }
    };

    /**
     * 获取数据的回调接口
     */
    private ICometCallback cometCallback = new ICometCallback() {
        @Override
        public void onMsgFormatError() {
        }

        @Override
        public void onMsgArrived(Message arg0) {
            // 接受到消息后重置计时
            if (null != iCometManager) {
                iCometManager.cometCount.set(0);
            }
        }

        @Override
        public void onErrorMsgArrived(Message arg0) {
        }

        @Override
        public void onDataMsgArrived(String content) {
            Log.i("onDataMsgArrived", content);
            if (null != handler) { // 接受到内容
                EntityBean respBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(content,
                        EntityBean.class);
                android.os.Message msg = android.os.Message.obtain();
                msg.what = REC_MSG;
                msg.obj = respBean;
                handler.sendMessage(msg);
            }
        }
    };

    private ChannelAllocator channel = new ChannelAllocator() {
        @Override
        public Channel allocate() {
            Channel channel = new Channel();
            channel.cname = appCaseno;
            channel.token = null;
            return channel;
        }
    };

    /**
     * 查询未推送的消息
     */
    private void searchUnreceiveMsg() {
        if (type == 0) {
            searchMsg("1");
        } else if (type == 1) {
            searchMsg("1");
        } else if (type == 2) {
            searchMsg("2");
        } else if (type == 3) {
            searchMsg("2");
        }
    }

    private void searchMsg(String msgType) {
        String userName = application.getKckpName();
        String token = application.getToken();
        ApiImpI.getInstance(application).searchUnMsg(appCaseno, msgType, userName, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {

                    }

                    @Override
                    public void onSuccess(Object result) {
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {

                        } else {
                            String redes = bean.getString("redes");
                            UiUtil.showToast(context, redes);
                        }
                    }
                });
    }

    /**
     * 接收到消息后的回调
     */
    private void receiveMsgCallback(final EntityBean respBean) {
        if (null == respBean) {
            return;
        }
        String msgType = respBean.getString("msgtype");
        String msgId = respBean.getString("id");
        String userName = application.getKckpName();
        String token = application.getToken();
        ApiImpI.getInstance(application).receiveCallBack(msgId, msgType, userName, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {

                    }

                    @Override
                    public void onSuccess(Object result) {
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {//接收成功
                            dealMsg(respBean);
                        } else {
                            String redes = bean.getString("redes");
                            UiUtil.showToast(context, redes);
                        }
                    }
                });
    }


    /**
     * 根据接受的推送消息进行后续处理
     *
     * @param respBean
     */
    private void dealMsg(EntityBean respBean) {
        try {
            Thread.sleep(1000);
            android.os.Message msg = android.os.Message.obtain();
            String msgType = respBean.getString("msgtype");
            EntityBean msgBean = respBean.getBean("msgdata");
            if (TextUtils.equals(msgType, "1")) {//定责照片审核
                String isqualify = msgBean.getString("isqualify");
                if (TextUtils.equals(isqualify, "1")) {//审核成功
                    String isSingle = msgBean.getString("issingle");
                    msg.what = REC_SUCCESS;
                    msg.arg1 = 1;
                    msg.obj = isSingle;
                } else {
                    msg.what = REC_FAIL;
                    msg.obj = msgBean;
                }
            } else if (TextUtils.equals(msgType, "2")) {//定责消息
                EntityBean[] dutyBeans = msgBean.getBeans("dutylist");
                msg.what = REC_SUCCESS;
                msg.arg1 = 2;
                msg.obj = dutyBeans;
            } else if (TextUtils.equals(msgType, "3")) {//定损照片审核
                String isqualify = msgBean.getString("isqualify");
                if (TextUtils.equals(isqualify, "1")) {//审核成功
                    msg.what = REC_SUCCESS;
                    msg.arg1 = 3;
                } else {
                    msg.what = REC_FAIL;
                    msg.obj = msgBean;
                }
            } else if (TextUtils.equals(msgType, "4")) {//在线定损判断
                String isass = msgBean.getString("isass");
                if (TextUtils.equals(isass, "1")) {//符合在线定损
                    msg.what = REC_SUCCESS;
                    msg.arg1 = 4;
                } else {
                    msg.what = REC_FAIL;
                    msg.obj = msgBean;
                }
            } else if (TextUtils.equals(msgType, "5")) {//定损消息
                msg.what = REC_SUCCESS;
                msg.arg1 = 5;
                msg.obj = msgBean;
            } else if (TextUtils.equals(msgType, "6")) {//赔偿消息
                msg.what = REC_SUCCESS;
                msg.arg1 = 6;
                msg.obj = msgBean;
            } else if (TextUtils.equals(msgType, "7")) {//现场处理或撤销案件
                String state = msgBean.getString("casestate");
                msg.what = CANCEL_MSG;
                msg.obj = state;
            }
            if (null != handler) {
                handler.sendMessage(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 启动定时器
     */
    private void startTimer() {
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                time--;// 减少等待时间
                handler.sendEmptyMessage(TIME);
            }
        };
        // 每隔1秒更新一次
        timer.schedule(task, 0, 1000);
    }

    /**
     * 关闭定时器
     */
    private void stopTimer() {
        if (task != null) {
            task.cancel();
        }
        task = null;
        if (timer != null) {
            timer.cancel();
        }
        timer = null;
        time = 180;
    }

    /**
     * 根据监听结果显示后续的处理界面
     *
     * @param isFinish：是否有审核结果
     * @param bean：审核返回的结果
     */
    private void showView(boolean isFinish, EntityBean bean) {
        if (null == view) {
            return;
        }
        waitLayout.setVisibility(View.GONE);
        dealLayout.setVisibility(View.VISIBLE);
        if (type == 0) {//事故审核
            if (isFinish) {//审核失败
                infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.tra_review03));
                String notice = bean.getString("notice");
                String remarks = bean.getString("remarks");
                if(null!=notice){
                    infoTV.setText(notice+"\n"+"由于以上原因，"+remarks+"交警无法判定责任，您可以选择：");
                }else {
                    infoTV.setText("照片不清晰"+"\n"+"由于以上原因，"+remarks+"交警无法判定责任，您可以选择：");
                }
                dealOBtn.setVisibility(View.VISIBLE);
                dealOBtn.setText("重新拍照上传");
                dealOBtn.setTag(bean);
            } else {//审核时间太长
                infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.tra_review02));
                infoTV.setText(R.string.accident_check_fail);
                dealOBtn.setText("继续等待！");
            }
            dealTBtn.setText("直接报警！");
        } else if (type == 1) {
            if (isFinish) {//审核失败
                infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.tra_review03));
                String notice = bean.getString("notice");
                String remarks = bean.getString("remarks");
                if(null!=notice){
                    infoTV.setText(notice+"\n"+"由于以上原因，"+remarks+"交警无法判定责任，您可以选择：");
                }else {
                    infoTV.setText("照片不清晰"+"\n"+"由于以上原因，"+remarks+"交警无法判定责任，您可以选择：");
                }
                dealOBtn.setVisibility(View.VISIBLE);
                dealOBtn.setText("重新拍照上传");
                dealOBtn.setTag(bean);
            } else {//审核时间太长
                infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.tra_review02));
                infoTV.setText(R.string.accident_check_fail);
                dealOBtn.setText("继续等待！");
            }
            dealTBtn.setText("直接报警！");
        }
    }

    //现场处理或者撤销案件
    private void showCancelView(String state) {
        isFinish = true;
        infoIV.setImageBitmap(CommontUtils.readBitmap(context, R.mipmap.tra_review02));
        String info = context.getResources().getString(R.string.accident_cancel_info);
        if (TextUtils.equals(state, "0")) {
            infoTV.setText(String.format(info, "现场处理"));
        } else if (TextUtils.equals(state, "1")) {
            infoTV.setText(String.format(info, "撤销案件"));
        }
        dealOBtn.setVisibility(View.GONE);
        dealTBtn.setText("直接报警！");
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TIME:
                    timeTV.setText(String.valueOf(time));
                    if (time == 0) {//等待时间太长
                        stopTimer();
                        showView(false, null);
                    }
                    break;
                case REC_SUCCESS:
                    if (type == 0 && msg.arg1 == 1) {//定责图片审核通过提示挪车
                        PromptAfterCreateAppointView view =
                                new PromptAfterCreateAppointView(context, appCaseno);
                        view.setIsSingleCar((String) msg.obj);
                        FrameworkManager.getInstance().showNewForm(context, view);
                        OnDestroy();
                        closeForm();
                    } else if (type == 1 && msg.arg1 == 1) {
                        PromptAfterCreateAppointView view =
                                new PromptAfterCreateAppointView(context, appCaseno);
                        String obj = (String) msg.obj;
                        if("10".equals(obj)){
                            view.setIsSingleCar("2");
                        }else {
                            view.setIsSingleCar("3");
                        }
                        FrameworkManager.getInstance().showNewForm(context, view);
                        OnDestroy();
                        closeForm();
                    } else if (type == 2 && msg.arg1 == 2) {//定责消息生成协议书
                        EntityBean[] beans = (EntityBean[]) msg.obj;
                        CreateAgreementView createagreementview =
                                new CreateAgreementView(context, beans, appCaseno,1);
                        FrameworkManager.getInstance().showNewForm(context, createagreementview);
                        OnDestroy();
                        closeForm();
                    } else if (type == 3 && msg.arg1 == 2) {
                        EntityBean[] beans = (EntityBean[]) msg.obj;
                        CreateAgreementView createagreementview2 = new CreateAgreementView(context, beans, appCaseno,4);
                        FrameworkManager.getInstance().showNewForm(context, createagreementview2);
                        OnDestroy();
                        closeForm();
                    }
                    break;
                case REC_FAIL://审核失败
                    stopListening();
                    showView(true, (EntityBean) msg.obj);
                    break;
                case SEARCH_MSG:
                    searchUnreceiveMsg();
                    break;
                case REC_MSG:
                    receiveMsgCallback((EntityBean) msg.obj);
                    break;
                case CANCEL_MSG:
                    stopListening();
                    showCancelView((String) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        waitLayout = null;
        dealLayout = null;
        regEvent(false);
        stopListening();
        handler = null;
        removeILFMsgListener(this);
    }
}